package com.atguigu.educms.service.impl;

import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.educms.entity.CmsComment;
import com.atguigu.educms.entity.CmsContent;
import com.atguigu.educms.mapper.CmsCommentMapper;
import com.atguigu.educms.service.CmsCommentService;
import com.atguigu.educms.service.CmsContentService;
import com.atguigu.educms.util.PageFrontUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章评论 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-03-11
 */
@Service
public class CmsCommentServiceImpl extends ServiceImpl<CmsCommentMapper, CmsComment> implements CmsCommentService {

    @Autowired
    private CmsContentService contentService;

    @Override
    public FrontPageBean getFrontComment(Integer pageNo, Integer pageSize, String contentId) {
        Page<CmsComment> pageParam = new Page<>(pageNo,pageSize);
        QueryWrapper<CmsComment> qw = new QueryWrapper<>();
        qw.eq("content_id",contentId);
        qw.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, qw);
        return PageFrontUtils.copyPropertiesFrontPage(pageParam);
    }

    @Override
    public void commitComment(CmsComment cmsComment) {
        baseMapper.insert(cmsComment);
        updateCommentNum(cmsComment.getContentId());
    }

    /**
     * 评论后 更新文章评论个数
     * @param contentId
     * @return
     */
    public void updateCommentNum(Integer contentId){
        Integer commentNum = baseMapper.selectCount(new QueryWrapper<CmsComment>().eq("contentId", contentId));
        contentService.editCommentNumById(contentId,commentNum);
    }

}
